<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <link rel="stylesheet" href="./font/iconfont.css">
    <style type="text/css">
        body,
        html {
            width: 100%;
            height: 100%;
            overflow: hidden;
            margin: 0;
            font-family: "微软雅黑";
        }

        #allmap {
            width: 100%;
            height: 100%;
        }

        .queryContainer {
            text-align: center;
            line-height: 50px;
            width: 100px;
            height: 50px;
            background: #1e9ee9;
            border-radius: 15px;
            opacity: 0.8;
            left: 80px;
            top: 20px;
            position: absolute;
        }
    </style>
    <script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=你的密钥"></script>
    <title>百度地图demo</title>
</head>

<body>
    <div id="allmap"></div>
    <div class="queryContainer">
        <button onclick="locating()">定位</button>
    </div>
</body>

</html>
<script type="text/javascript">

    let map = new BMap.Map("allmap");    // 创建Map实例

    map.setCurrentCity("南京");          // 设置地图显示的城市 此项是必须设置的
    map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放

    map.centerAndZoom(new BMap.Point(118.797248, 32.025476), 18);
    // 定位方式:
    // 根据浏览器定位进行定位
    function locating() {
        let geolocation = new BMap.Geolocation();
        geolocation.getCurrentPosition(function (current) {
            console.log(current);
            if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                let mk = new BMap.Marker(current.point);
                map.addOverlay(mk);
                map.panTo(current.point);
            }
            else {
                alert('failed' + this.getStatus());
            }
        }, { enableHighAccuracy: true })
    }
</script>